package com.ruoyi.wms.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import com.ruoyi.wms.service.IWmsMaterialStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsMaterialIncome;
import com.ruoyi.wms.service.IWmsMaterialIncomeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料入库Controller
 * 
 * @author chao
 * @date 2024-07-23
 */
@Controller
@RequestMapping("/wms/material_income")
public class WmsMaterialIncomeController extends BaseController
{
    private String prefix = "wms/material_income";

    @Autowired
    private IWmsMaterialIncomeService wmsMaterialIncomeService;

    @Autowired
    private IMainUnitesService mainUnitesService;

    @RequiresPermissions("wms:material_income:view")
    @GetMapping()
    public String material_income()
    {
        return prefix + "/material_income";
    }

    /**
     * 查询物料入库列表
     */
    @RequiresPermissions("wms:material_income:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsMaterialIncome wmsMaterialIncome)
    {
        startPage();
        List<WmsMaterialIncome> list = wmsMaterialIncomeService.selectWmsMaterialIncomeList(wmsMaterialIncome);
        return getDataTable(list);
    }

    /**
     * 导出物料入库列表
     */
    @RequiresPermissions("wms:material_income:export")
    @Log(title = "物料入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsMaterialIncome wmsMaterialIncome)
    {
        List<WmsMaterialIncome> list = wmsMaterialIncomeService.selectWmsMaterialIncomeList(wmsMaterialIncome);
        ExcelUtil<WmsMaterialIncome> util = new ExcelUtil<WmsMaterialIncome>(WmsMaterialIncome.class);
        return util.exportExcel(list, "物料入库数据");
    }

    /**
     * 新增物料入库
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/add";
    }

    /**
     * 新增保存物料入库
     */
    @RequiresPermissions("wms:material_income:add")
    @Log(title = "物料入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsMaterialIncome wmsMaterialIncome)
    {
        return toAjax(wmsMaterialIncomeService.insertWmsMaterialIncome(wmsMaterialIncome));
    }

    /**
     * 修改物料入库
     */
    @RequiresPermissions("wms:material_income:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsMaterialIncome wmsMaterialIncome = wmsMaterialIncomeService.selectWmsMaterialIncomeById(id);
        mmap.put("wmsMaterialIncome", wmsMaterialIncome);
        mmap.put("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/edit";
    }

    /**
     * 修改保存物料入库
     */
    @RequiresPermissions("wms:material_income:edit")
    @Log(title = "物料入库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsMaterialIncome wmsMaterialIncome,Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return toAjax(wmsMaterialIncomeService.updateWmsMaterialIncome(wmsMaterialIncome));
    }

    /**
     * 删除物料入库
     */
    @RequiresPermissions("wms:material_income:remove")
    @Log(title = "物料入库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsMaterialIncomeService.deleteWmsMaterialIncomeByIds(ids));
    }

    @Log(title = "执行入库", businessType = BusinessType.OTHER)
    @PostMapping("/executeStorage")
    @ResponseBody
    public AjaxResult storge(@RequestParam("Rowid") Long Rowid, @RequestParam("Materialid") Long Materialid, @RequestParam("num") Double num){
        try {
            wmsMaterialIncomeService.executeStorage(Rowid,Materialid,num);
            return AjaxResult.success("入库成功");
        } catch (Exception e) {
            return AjaxResult.error("入库失败：" + e.getMessage());
        }
    }

}
